import pygame
from random import randint

pygame.init()

WIN_WIDTH = 400
WIN_HEIGHT = 600
window = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('控制移动')
window.fill((255, 255, 255))
pygame.display.flip()

# 放置图片
rabbit_img = pygame.image.load('files/WechatIMG1278.jpeg')
new_rabbit_img = pygame.transform.scale(rabbit_img, (50, 50))
rabbit_x, rabbit_y = 100, 300
window.blit(new_rabbit_img, (rabbit_x, rabbit_y))
pygame.display.update()

is_move = False
num = 0
y_step = 0
x_step = 0

while True:
    if is_move == True:
        num += 1
        if num % 200 == 0:
            window.fill((255, 255, 255))
            rabbit_y += y_step
            rabbit_x += x_step
            window.blit(new_rabbit_img, (rabbit_x, rabbit_y))
            pygame.display.update()
            print(num, 'wo')

    # 检测事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        if event.type == pygame.KEYDOWN:
            code = event.key
            print(code)
            if code == 119:
                is_move = True
                y_step = -1
                x_step = 0
            elif code == 115:
                is_move = True
                y_step = 1
                x_step = 0
            elif code == 97:
                is_move = True
                x_step = -1
                y_step = 0
            elif code == 100:
                is_move = True
                x_step = 1
                y_step = 0

        if event.type == pygame.KEYUP:
            is_move = False
